package com.cy.aircompressor.main.home.other.provider;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.cy.aircompressor.databinding.ItemWarningConfigLayoutBinding;
import com.cy.aircompressor.entity.warning.WarningConfig;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent;
import com.net.model.base.BaseBindViewHolder;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class WarningConfigProvider extends ItemViewBinder<WarningConfig, WarningConfigProvider.ViewHolder> {
    Context mContext;
    public WarningConfigProvider(Context mContext) {
        this.mContext = mContext;

    }

    @NonNull
    @Override
    protected WarningConfigProvider.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemWarningConfigLayoutBinding.inflate(inflater, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull WarningConfigProvider.ViewHolder holder, @NonNull WarningConfig entity) {
        holder.mBinding.serial.setText(holder.getLayoutPosition() + "");
        holder.mBinding.compressorPart.setText(entity.getCompressorPartDesc());
        holder.mBinding.triggerCount.setText(entity.getTriggerCount() + "");
        holder.mBinding.minValue.setText(entity.getMinValue() + "");
        holder.mBinding.maxValue.setText(entity.getMaxValue() + "");
        RxTextView.textChangeEvents(holder.mBinding.triggerCount)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TextViewTextChangeEvent>() {
                    @Override
                    public void accept(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
                        String input = holder.mBinding.triggerCount.getText().toString().trim();
                        if (TextUtils.isEmpty(input)) return;
                        int num = Integer.parseInt(input);
                        if (num == entity.getTriggerCount()) return;
                        entity.setTriggerCount(num);
                        entity.setSelect(true);
                    }
                });
        RxTextView.textChangeEvents(holder.mBinding.minValue)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TextViewTextChangeEvent>() {
                    @Override
                    public void accept(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
                        String input = holder.mBinding.minValue.getText().toString().trim();
                        if (TextUtils.isEmpty(input)) return;
                        int num = Integer.parseInt(input);
                        if (num == entity.getMinValue()) return;
                        entity.setMinValue(num);
                        entity.setSelect(true);
                    }
                });
        RxTextView.textChangeEvents(holder.mBinding.maxValue)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TextViewTextChangeEvent>() {
                    @Override
                    public void accept(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
                        String input = holder.mBinding.maxValue.getText().toString().trim();
                        if (TextUtils.isEmpty(input)) return;
                        int num = Integer.parseInt(input);
                        if (num == entity.getMaxValue()) return;
                        entity.setMaxValue(num);
                        entity.setSelect(true);
                    }
                });


    }

    public class ViewHolder extends BaseBindViewHolder<ItemWarningConfigLayoutBinding> {

        protected ViewHolder(ItemWarningConfigLayoutBinding itemView) {
            super(itemView);

        }
    }


}
